<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="关键词名称" prop="wordName">
      <el-input v-model="dataForm.wordName" placeholder="关键词名称"></el-input>
    </el-form-item>
    <!-- <el-form-item label="有效标识" prop="deletedAt">
      <el-input v-model="dataForm.deletedAt" placeholder="有效标识"></el-input>
    </el-form-item>
    <el-form-item label="热度（搜索量，当前使用搜索次数来表示热度）" prop="popularity">
      <el-input v-model="dataForm.popularity" placeholder="热度（搜索量，当前使用搜索次数来表示热度）"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder="更新时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          wordName: '',
          deletedAt: '',
          popularity: '',
          description: '',
          createdAt: '',
          updatedAt: ''
        },
        dataRule: {
          wordName: [
            { required: true, message: '关键词名称不能为空', trigger: 'blur' }
          ],
          deletedAt: [
            { required: true, message: '有效标识不能为空', trigger: 'blur' }
          ],
          popularity: [
            { required: true, message: '热度（搜索量，当前使用搜索次数来表示热度）不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/biz/keyWordsLibrary/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.wordName = data.keyWordsLibrary.wordName
                this.dataForm.deletedAt = data.keyWordsLibrary.deletedAt
                this.dataForm.popularity = data.keyWordsLibrary.popularity
                this.dataForm.description = data.keyWordsLibrary.description
                this.dataForm.createdAt = data.keyWordsLibrary.createdAt
                this.dataForm.updatedAt = data.keyWordsLibrary.updatedAt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz/keyWordsLibrary/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'wordName': this.dataForm.wordName,
                'deletedAt': this.dataForm.deletedAt,
                'popularity': this.dataForm.popularity,
                'description': this.dataForm.description,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
